<?php

namespace app\admin\controller;

//use think\Controller;
use think\Request;
use think\Db;
use think\File;


class Musics extends Admin
{
    public function __construct(){
        parent::initialize();
        // var_dump($this->initialize()) ;

    }

    
    public function index()
    {
        $list = Db::name('tb_musics')->field(['id','img','name'])->paginate(3);
        return $this->fetch('musics/musics',[
                      'list'=>$list
        ]);
    }


//显示添加页面
    public function addmusic()
    {	
    	$list = Db::name('tb_music_classify')->where('parent_id',0)->field(['name', 'id'])->select();
    	
    	foreach ($list as $k=>$v) {


    		
    		$data = Db::name('tb_music_classify')->where('parent_id',$v['id'])->field(['name', 'id'])->select();
    		
    		
    		foreach($data as $kk=>$vv){
    			$list[$k]['type'][] = $data[$kk];
    		}
    		
    	}

    	
    	
    	
    	return $this->fetch('addmusics/addmusics',[
    			'list'=>$list,
    		]);
    }

    public function add(Request $request)
    {
        $data = Request::instance()->post();
       

		$image = request()->file('image');
		$lyrics = request()->file('lyrics');

     
    	// var_dump($data,$image,$music,$lyrics);

    	if ($image&&$lyrics){

    	    $img = $image->validate(['size'=>156780000,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads/music-img');
    
    	    $lrc = $lyrics->validate(['size'=>156780000,'ext'=>'lrc'])->move(ROOT_PATH . 'public' . DS . 'uploads/music-lrc');

    	    $singer = Db::name('tb_singers')->where('name',$data['singer'])->value('id');
    	    $specials = Db::name('tb_specials')->where('name',$data['specials'])->value('id');

    	}

    	if ($singer&&$specials) {

    		$img = $img->getSaveName();
            $img = str_replace('\\', '/', $img);
            //图片地址
          
            $lrc = $lrc->getSaveName();
            $lrc = str_replace('\\', '/', $lrc);
            //歌词地址
    		$result=Db::table('tb_musics')
    		    ->data(['name'=>$data['name'],'typeid'=>$data['26'],'singerid'=>$singer,'specialid'=>$specials,'languageid'=>$data['27'],'feelid'=>$data['25'],'createtime'=>time(),'clicknum'=>0,'Lyrics'=>$lrc,'musicaddress'=>$data['music'],'img'=>$img])
    		    ->insert();
    	}else{
    		$this->error('请查看添加专辑和歌手是否正确');
    	}


    	if ($result) {
    		$this->success('添加成功','index');
    	}else{
    		$this->error('不能为空');
    	}



    }


    public function specials(Request $request)
    {
    	$data = Request::instance()->post();

        $list = Db::name('tb_specials')->where('name',$data['specials'])->value('id');
        echo json_encode($list);
    }

    public function editmusic($id)
    {
        $list=Db::table('tb_musics')
            ->alias('a')
            ->where('a.id',$id)
            ->join('tb_singers s','a.singerid = s.id')
            ->field(['a.id as aid','s.id as sid','a.name as aname','s.name as sname','musicaddress','Lyrics','img'])
            ->select();
        
        return $this->fetch('musics/editmusic',['list'=>$list[0]]);

    }

    public function edit(Request $request)
    {
        $data = Request::instance()->post();
        // var_dump($data);
        $image = request()->file('image');
        $lyrics = request()->file('lyrics');
        $i = 0;
        if ($image) {
            $img = $image->validate(['size'=>156780000,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads/music-img');
            $img = $img->getSaveName();
            $img = str_replace('\\', '/', $img);           
            $list = Db::name('tb_musics')->where('id',$data['id'])->update(['img'=>$img]);
            $i++;
        }
        if ($lyrics) {
            $lrc = $lyrics->validate(['size'=>156780000,'ext'=>'lrc'])->move(ROOT_PATH . 'public' . DS . 'uploads/music-lrc');
            $lrc = $lrc->getSaveName();
            $lrc = str_replace('\\', '/', $lrc);
            $list = Db::name('tb_musics')->where('id',$data['id'])->update(['Lyrics'=>$lrc]);
            $i++;
        }
        if ($data) {
            $list = Db::name('tb_musics')->where('id',$data['id'])->update(['name'=>$data['name'],'singerid'=>$data['sid']]);
            $i++;
        };
        if ($i>0) {
            return $this->success('编辑成功','index');
        }else{
            return $this->error('编辑失败');
        }
    }

    public function del(Request $request)
    {
        $data = Request::instance()->post();
        $list = Db::name('tb_musics')->where('id',$data['id'])->delete();
        return $list;
    }
}
